package com.gzy.rpsm_property.controller;

import com.gzy.rpsm_property.pojo.ResponsePojo;
import com.gzy.rpsm_property.pojo.User;
import com.gzy.rpsm_property.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Optional;

@RestController
@Api(tags="用户相关接口")
public class UserController {
    @Autowired
    UserService userService;
    @PutMapping("updateStatus/{userid}")
    @ApiOperation(value = "修改用户状态，主要更换业主后",
            produces = MediaType.APPLICATION_JSON_VALUE)

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", type = "Integer", readOnly = true, paramType = "path"),
            @ApiImplicitParam(name = "user_status", type = "short", readOnly = true, paramType = "query"),

    })
    public ResponsePojo<Integer> changeUserStatus(@ApiIgnore User user) {
        int result = userService.changeUserStatus(user);
        if (result == 0) {
            return ResponsePojo.fail(result, "该用户id失效");

        }
        return ResponsePojo.success(result);
    }

    @GetMapping("/id/{userid}")
    @ApiOperation( value="根据用户 id 查询用户详细信息",
            notes="<span style='color:red'>若查询失败，返回null</span>",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userid",type =
                    "Integer",required = true)
    })
    public ResponsePojo<User> queryById(@PathVariable("userid")Integer id){
        User u = userService.queryById(id);
        if(Optional.ofNullable(u).isPresent()){
            return ResponsePojo.success(u) ;
        }
        return ResponsePojo.fail(u,"该 id 不存在对应用户");
    }
}
